/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kleinberg;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author veena
 */
public class burstExtraction {
    
    String dfile="/home/veena/veena/f14/660/TG/obamacare/mitt2012/costfunc";

    public void readcosts(){
        BufferedReader buff=null;
        SingleStream sst=new SingleStream();
        burstExtraction be=new burstExtraction();
        //read offsets first
        int[] offsets=null;
        offsets=sst.readminutes();
        //next read in costs
        try {
        
        buff = new BufferedReader(new FileReader((dfile)));
        String s=null;String[] spl=null;
        double minutes=0;
        ArrayList<Double> vals=new ArrayList<>();
        try {
            while((s=buff.readLine())!=null){
                spl=s.split(",");
                for(int i=0;i<spl.length;i++){
                minutes=Double.valueOf(spl[i].trim());
                vals.add(minutes);
//                System.out.println(minutes+" "+spl[1]);
                }
                
            }
            buff.close();
            be.getburst(vals, offsets);
        } catch (IOException ex) {
            Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
    }finally{
            try {
                buff.close();
            } catch (IOException ex) {
                Logger.getLogger(burstExtraction.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private void getburst(ArrayList<Double> tmp,int[] offsets){
        ArrayList<String> bursts=new ArrayList<String>();
        String s1="",s2="",s3="";
        Double d1=0.0,d2=0.0;
        boolean condition=false;
        String[] spl1=null;
        for(int i=0;i<tmp.size();i++){
                    if(i==0){
                        s1=String.valueOf(1.0)+":"+offsets[i]+":";
                        bursts.add(s1);
                    }else{
                    //nested substructure of bursts
                        d1=tmp.get(i);
//                        System.out.println(d1);
                        if(i+1<tmp.size()-1)
                            d2=tmp.get(i+1);
//                        System.out.println(d2);
                        if(d2>d1){
    //                        
                            s2=String.valueOf(d2)+":"+offsets[i+1]+":";
//                            System.out.println(d2+"-"+offsets[i+1]);
                            bursts.add(s2);
                        }else if(d2<d1){
                            //ca be new level beginning here other than the ones present
                            //check if level not already present then add or else 
                            //if exists prev level continuing
                            for(int h=0;h<bursts.size();h++){
                                spl1=bursts.get(h).replace("\"", "").split(":");
                                if(spl1.length<3){
                                    if(Objects.equals(Double.valueOf(spl1[0]), d2)){
                                        condition=true;
                                        break;
                                    }
                                }
                            }
                            spl1=null;
                           if(condition){
                            //OR...
                        //level decreased-mark the end here of prev open 
                            //check if end is already appended or else append it
                            for(int h=0;h<bursts.size();h++){
                                spl1=bursts.get(h).split(":");
                                if(spl1.length<3){
                                    if(d2>Double.valueOf(spl1[0])){
                                        //not end of found val

                                    }else if(String.valueOf(d2).trim().equals(String.valueOf(spl1[0].trim()))){
                                    //not end of current val still continuing

                                    }else {
                                        s3=bursts.get(h);
                                        if(i+1<tmp.size()-1)
                                        s3+=offsets[i+1];
                                        bursts.set(h, s3);
//                                        System.out.println(h+"-"+s3);
                                        
                                        }
                                }
                            }
                        }else{
                               //anther level not in list added
                               s2=String.valueOf(d2)+":"+offsets[i+1]+":";
//                            System.out.println(d2+"-"+offsets[i+1]);
                                bursts.add(s2);
                               //close levels greater than this
                                for(int h=0;h<bursts.size();h++){
                                spl1=bursts.get(h).split(":");
                                    if(spl1.length<3){
                                        if(d2<Double.valueOf(spl1[0])){
                                        //close events greater than this
                                            s3=bursts.get(h);
                                        if(i+1<tmp.size()-1)
                                        s3+=offsets[i+1];
                                        bursts.set(h, s3);
//                                        System.out.println(h+"-"+s3);
                                    }


                                    }
                                }
                           }
                            condition=false;
                            }//else its same state
                            spl1=null;condition=false;
                            if(i==tmp.size()-1){
                                s1=bursts.get(0);
                                s1+=offsets[offsets.length-1];
                                
                                bursts.set(0, s1);
                            }
                    }
            }
        long start=0,end=0;
SimpleDateFormat dat=new SimpleDateFormat("EEE MMM dd HH:mm:ss +SSSS yyyy");
Date date1=null,date2=null;
String dat1,dat2;
Calendar cal=Calendar.getInstance();
for(int i=0;i<bursts.size();i++){
                   spl1=bursts.get(0).split(":");
                   s3=spl1[2].trim();
                   spl1=bursts.get(i).split(":");
                   if(spl1.length<3){
                   //fill it up with missing last entry
                       s2=bursts.get(i);
                       s2+=s3;
                       bursts.set(i, s2);
                    }
//                   System.out.println(bursts.get(i));
                   //convert to dates and print here
                   spl1=null;
                   spl1=bursts.get(i).split(":");
                   cal.setTimeInMillis(Long.valueOf(spl1[1])*1000);
                   date1=cal.getTime();
                   dat1=dat.format(date1);
                   cal.setTimeInMillis(Long.valueOf(spl1[2])*1000);
                   date2=cal.getTime();
                   dat2=dat.format(date2);
                   System.out.println(spl1[0]+"-"+dat1+"-"+dat2);
                   spl1=null;
               }
        
    }
}
